Skip to content

Conversation

@amotl
Copy link
Member

@amotl amotl commented Jul 22, 2022

Motivation

About

datetime.date.today() returns the current local date. So, its outcome depends on the system time zone. Because we want to use UTC-based date and time objects instead, datetime.utcnow().date() is a better choice.

@amotl amotl force-pushed the amo/fix-date-today branch 5 times, most recently from ee86c14 to 07b8d79 Compare July 22, 2022 23:18
@amotl amotl marked this pull request as ready for review July 22, 2022 23:28
@amotl amotl requested review from matriv and mfussenegger July 22, 2022 23:29
Copy link
Member

@mfussenegger mfussenegger left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Other than the test addition this looks good to me.

The commit message could be clarified a bit. Using UTC is not automatically better - it depends on the use case.

What is a problem is that we were comparing utc variants with local variants in

>>> from datetime import datetime
>>> now = datetime.utcnow()
>>> dt = location.date
>>> dt.year == now.year
True
>>> dt.month == now.month
True
>>> dt.day == now.day
True
>>> (now - location.datetime_tz).seconds < 4
True

That is the problem and caused test flakyness.

import time_machine


class DateTodayTest(TestCase):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please remove, there's no need to create tests that verify documented behavior of the standard library.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks. I've removed the test cases and amended the commit message.

`datetime.date.today()` returns the current local date. In order to
reduce test flakyness when comparing UTC-based datetimes with localtime-
based variants, `datetime.utcnow().date()` is a better choice.
@amotl amotl force-pushed the amo/fix-date-today branch from 07b8d79 to 4cf9d00 Compare July 25, 2022 07:46
@amotl amotl requested a review from mfussenegger July 25, 2022 07:49
@amotl amotl merged commit 17bda3e into master Jul 25, 2022
@amotl amotl deleted the amo/fix-date-today branch July 25, 2022 08:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants